﻿/**
 * Created by Administrator on 2017/8/18 0018.
 */
var express = require('express');
var crypto = require("crypto");
var qr = require('qr-image')
var multer = require("multer");
var path = require('path')
var fs = require('fs')
var config = require('../../config.js');
var db = require('../../db/db.js');
var moment = require("moment");

var router = express.Router();
var file_model = require('../../model/yoshop_upload_file.js');
var goods_image_model = require("../../model/yoshop_goods_image");



router.post('/add', async function (req, res) {

    var data = req.body;
    data.createtime = new Date();
    var obj = await file_model.add(data);
    if (obj.code == 200) {
        var ss = await goods_image_model.add({ goods_id: req.body.goods_id, image_id: obj.msg.insertId, create_time: new Date() });

        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "error"
        }
        res.jsonp(result);
    }


})


router.post("/edit", async function (req, res) {

    var data = req.body;
    data.createtime = new Date();
    var obj = await file_model.edit(data);
    if (obj.code == 200) {
        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "error"
        }
        res.jsonp(result);
    }

})

router.post('/list', async function (req, res) {
    var page;
    var rows;
    if (isNaN(req.body.page)) {
        page = 1;
    } else {
        page = +req.body.page;
    }
    if (isNaN(req.body.rows)) {
        rows = 10;
    } else {
        rows = +req.body.rows;
    }
    var first = rows * (page - 1);

    var where = " 1=1 ";
    var list = [];
    var count = 0;
    if (typeof (req.body.name) != "undefined" && req.body.name != "") {
        where = where + " and exam.name like  '%" + req.body.name + "%'";

    }
    if (typeof (req.body.subject) != "undefined" && req.body.subject != "") {
        where = where + " and exam.subject='" + req.body.subject + "'";

    }
    if (typeof (req.body.gradeid) != "undefined" && req.body.gradeid != "") {
        where = where + " and exam.gradeid=" + req.body.gradeid;

    }
    if (typeof (req.body.goods_id) != "undefined" && req.body.goods_id != "") {
        where = where + " and yoshop_goods_image.goods_id=" + req.body.goods_id;
        list = await file_model.getlist(rows, first, where, " join yoshop_goods_image on yoshop_goods_image.image_id=yoshop_upload_file.id", "", "yoshop_upload_file.*");
        count = await file_model.getcount(where, " join yoshop_goods_image on yoshop_goods_image.image_id=yoshop_upload_file.id");
    } else {

        list = await file_model.getlist(rows, first, where);
        count = await file_model.getcount(where);

    }



    console.log("where", where);

    for (var i = 0; i < list.length; i++) {


        list[i].create_time = moment(list[i].create_time).format("YYYY-MM-DD HH:mm:ss");


    }

    var allcount = parseInt(parseInt(count) / rows) + 1;
    if (count % rows == 0) {
        allcount = allcount - 1;
    }

    res.send({ status: 200, data: list, message: "", allcount: allcount, count: count });


});




router.delete('/del', async function (req, res) {

    var where = " id in (" + req.body.ids + ")";
    var obj = await file_model.del(where);
    if (obj.code == 200) {
        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "success"
        }
        res.jsonp(result);
    }


});



module.exports = router;   /*暴露这个 router模块*/